package com.kfm.base.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class TenGame {

    private Poker poker;
    Scanner in = new Scanner(System.in);


    public void start(){
        // 获取扑克牌
        poker = new Poker();
        List<Poker.Card> pokers = poker.getPokers();

        // 洗牌
        Collections.shuffle(pokers);
        Collections.shuffle(pokers);
        Collections.shuffle(pokers);
        Collections.shuffle(pokers);

        int i = 0; // 扑克牌的第几张
        // 给玩家发牌
        List<Poker.Card> player1 = new ArrayList<>();
        // 点数
        double value = 0;
        int select = 0;
        do {
            System.out.println("给玩家发第【" + (player1.size() + 1) + "】张手牌");
            Poker.Card card = pokers.get(i ++);
            player1.add(card);
            System.out.println(player1);
            value += card.getValue();

            // 计算点数
            if (value > 10.5){
                System.out.println("boom！ 您输了");
                return;
            }

            if (player1.size() == 5){
                System.out.println("您获胜！");
                return;
            } else {
                System.out.println("是否继续发牌? 0.停止 1.继续");
                select = in.nextInt();
            }

        } while(select == 1 && player1.size() < 5);

        System.out.println("您的点数是：" + value);

        System.out.println("机器人开始：");
        List<Poker.Card> bot = new ArrayList<>();
        double botValue = 0;


        do{
            if (botValue != 0){
                System.out.println("继续发牌！");
            }
            System.out.println("机器人派第 " + (bot.size() + 1) + " 张牌");
            Poker.Card card = pokers.get(i ++);
            bot.add(card);
            System.out.println(bot);
            botValue += card.getValue();

            if (botValue > 10.5){
                System.out.println("boom！ 您赢了");
                return;
            }
            if (bot.size() == 5){
                System.out.println("您输了");
                return;
            }
        } while (botValue <= value);

        System.out.println("您的点数为：" + value);
        System.out.println("机器人点数为：" + botValue);
        System.out.println("很遗憾，您输了!");
    }

    public static void main(String[] args) {
        new TenGame().start();
    }
}

class Poker {

    private List<String> colors = List.of("♠", "♥", "♣", "♦");

    private List<String> points = List.of("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K");

    private List<Card> pokers;

    public Poker(){
        pokers = new ArrayList<>();
        for (int i = 0; i < colors.size(); i++) {
            for (int j = 0; j < points.size(); j++) {
                if (j < 10){
                    pokers.add(new Card(colors.get(i), points.get(j), j + 1.0));
                } else {
                    pokers.add(new Card(colors.get(i), points.get(j), 0.5));
                }
            }
        }
    }

    public List<Card> getPokers(){
        return pokers;
    }

    class Card {
        private String color;

        private String point;

        private Double value;

        public Card(String color, String point, Double value){
            this.color = color;
            this.point = point;
            this.value = value;
        }

        public Double getValue() {
            return value;
        }

        public String toString(){
            return color + point;
        }
    }
}
